package org.wu.framework.lazy.orm.database.jpa.repository.support.method;

import org.wu.framework.lazy.orm.database.jpa.repository.enums.MethodName2SQLType;
import org.wu.framework.lazy.orm.database.jpa.repository.enums.QueryType;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;


public class JpaDynamicMethodSupportFactory {


    protected final static ConcurrentHashMap<QueryType, LazyDynamicMethodSimpleQuerySupport> cache =
            new ConcurrentHashMap<>() {
                {
                    put(QueryType.INSERT, new LazyInsertMethodSupport());
                    put(QueryType.DELETE, new LazyDeleteMethodSupport());
                    put(QueryType.UPDATE, new LazyUpdateMethodSupport());
                    put(QueryType.SELECT, new LazySelectMethodSupport());
                }
            };

    /**
     * 通过方法获取sql
     *
     * @param method 方法
     */
    public static String createSqlByMethod(Method method) {
        String methodName = method.getName();
        MethodName2SQLType name2SQLType = Arrays
                .stream(MethodName2SQLType.values())
                .filter(methodName2SQLType -> methodName.startsWith(methodName2SQLType.getStartMethodNamePrefix()))
                .findFirst().get();

        QueryType queryType = name2SQLType.getQueryType();


        return cache.get(queryType).methodName2SQL(method);
    }


}


